//
// Created by wopy on 2020/9/1.
//

#include "Utils.hpp"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <cerrno>

using namespace std;

static int BUFFER_LEN = 8196 * 1024;

bool isFile(std::string path) {
    struct stat s;

    if (stat(path.c_str(), &s)) {
        return false;
    }

    if (S_ISREG(s.st_mode)) {
        return true;
    }

    return false;
}

void copyFile(std::string sPath, std::string dPath) {
    int ifd = ::open(sPath.c_str(), O_RDONLY);
    if (ifd < 0) {
        return;
    }

    int ofd = ::creat(dPath.c_str(), 0644);

    if (ofd != -1) {
        char *buff = new char[BUFFER_LEN];
        int n;
        do {
            n = ::read(ifd ,buff, BUFFER_LEN);
            write(ofd, buff, n);
        } while (n == BUFFER_LEN);
        close(ofd);
    } else {
        perror("creat");
        cout << dPath << endl;
    }

    close(ifd);
}
